﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Touch2Like.DAL;
using Touch2Like.Model;
using Touch2Like.SharedLibrary;
using System.Xml;

namespace Touch2Like.Web
{
    public partial class Default : System.Web.UI.Page
    {
        string browserguid = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
            Response.Expires = -1500;
            Response.CacheControl = "no-cache";

            if (Request.Cookies["BrowserGUID"] != null && Request.Cookies["BrowserGUID"].Value != null)
                browserguid = Request.Cookies["BrowserGUID"].Value;
            else
            {
                browserguid = Guid.NewGuid().ToString();
                HttpCookie browserguidcookie = new HttpCookie("BrowserGUID", browserguid);
                browserguidcookie.Expires = DateTime.Now.AddDays(365);
                Response.SetCookie(browserguidcookie);
            }

            if (!IsPostBack)
            {
            }
        }

        protected void btn_Submit_Click(object sender, EventArgs e)
        {
            string taguid = "";

            if (!string.IsNullOrEmpty(Request["tb_Code"]))
            {
                taguid = Request["tb_Code"].Trim();

                UserTagDAL tag = new UserTagDAL();
                UserTagInfo tagInfo = tag.GetUserTagByTagUID(taguid);

                if (tagInfo != null)
                {
                    ActivityDAL activity = new ActivityDAL();
                    ActivityInfo activityInfo = new ActivityInfo();
                    activityInfo.TagUID = tagInfo.TagUID;
                    activityInfo.TagContent = tagInfo.TagContent;
                    activityInfo.TagType = tagInfo.TagType;
                    activityInfo.TagDate = DateTime.Now;
                    activityInfo.DeviceID = "";
                    activityInfo.IMSINumber = "";
                    activityInfo.PhoneNo = 0;
                    activityInfo.PhoneModel = "";
                    activityInfo.Longitude = "";
                    activityInfo.Latitude = "";
                    activityInfo.BrowserGUID = browserguid;

                    int activityid = activity.AddActivity(activityInfo);

                    if (string.IsNullOrEmpty(tagInfo.TagContent))
                    {
                        if (tagInfo.TagType == "Touch2Rate")
                        {
                            Response.Write("<script>document.location.href='/Touch2Rate/Survey.aspx?activityid=" + activityid + "&taguid=" + tagInfo.TagUID + "';</script>");
                            return;
                        }

                    }
                    else
                    {
                        Response.Redirect(tagInfo.TagContent.ToString(), false);
                        return;
                    }

                }
            }

            lb_Msg.Text = "<div class='alert alert-error'>Code is invalid.</div>";
        }
    }
}